package org.dx.oho.controller.shopadmin;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dx.oho.dto.ShopExecution;
import org.dx.oho.entity.PersonInfo;
import org.dx.oho.entity.Shop;
import org.dx.oho.enums.ShopStateEnum;
import org.dx.oho.service.ShopService;
import dx.util.HttpServletRequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * create by duanx 2021/2/2
 *@description： 店铺管理
 *@classname: ShopController
 *@author: duanx
 *@date: 2021/2/2 23:05
 **/
@Controller
@RequestMapping("/shopmanagement")
public class ShopManagementController {
    private final ShopService shopService;
    private Logger logger = LoggerFactory.getLogger(ShopManagementController.class);
    public ShopManagementController(ShopService shopService) {
        this.shopService = shopService;
    }

    /**
     * 店铺注册，新增店铺
     * 用户登录后进行店铺注册操作，
     * 用户填写店铺信息，上传店铺图片，提交信息等待待审核
     * @param request http请求
     * @return 注册状态Json
     */
    @RequestMapping(value = "/registeredshop",method = RequestMethod.POST)
    @ResponseBody

    public Map<String ,Object> registeredShop(HttpServletRequest request){
        Map<String ,Object> modelMap = new HashMap<>(6);
        //1.接受并转化相应参数
        //解析request里的shopStr （提交的店铺注册信息）
        String shopStr = HttpServletRequestUtil.getString(request,"shopStr");
        ObjectMapper mapper = new ObjectMapper();
        Shop shop;
        try {
            //将shopStr转换成Shop类
            shop = mapper.readValue(shopStr,Shop.class);
        }catch (Exception e){
            //转换失败时抛异常，并返回错误信息
            modelMap.put("success",false);
            modelMap.put("errMsg",e.toString());
            logger.error("获取传入数据，解析转换错误");
            return modelMap;
        }
        //使用CommonsMultipartFile接收文件流
        CommonsMultipartFile shopImgFile;
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            shopImgFile = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","上传的图片不能为空");
            return modelMap;
        }
        //2.店铺注册
        //尽可能的不去依赖前一步的信息
        if (shop!=null&&shopImgFile!=null){
            PersonInfo ownerPersonInfo = new PersonInfo();
            //todo:正式要从session中获取当前用户
            ownerPersonInfo.setUserId(1L);
            shop.setPersonInfo(ownerPersonInfo);
            ShopExecution shopExecution;
            try {
                shopExecution = shopService.addShop(shop,shopImgFile.getInputStream(),shopImgFile.getOriginalFilename());
                if (shopExecution == null) {
                    modelMap.put("success",false);
                    modelMap.put("errMsg","获取结果失败！");
                    return modelMap;
                }
                if (shopExecution.getState() == ShopStateEnum.CHECK.getState()){
                    modelMap.put("success",true);
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg",shopExecution.getStateInfo());
                    return modelMap;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入店铺信息");
            return modelMap;
        }
        return modelMap;
    }
 }
